//
//  HomeTableViewController.swift
//  mySwift
//
//  Created by 高赛 on 16/9/14.
//  Copyright © 2016年 高赛. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        
        // 1. 如果未登录,设置未登录界面的信息
        if !userLogin {
            visitorView.setupVisitorInfo(isHome: true, imageName: "visitordiscover_feed_image_house", message: "关注一些人,看看这里有什么惊喜")
            
        }else {
            // 2. 初始化导航条
            setNav()
        }
    }
    
    private func setNav(){
        
        // 左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBtnClick))
        // 右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        // 标题按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("LDeath ", for: [])
        titleBtn.sizeToFit()
        titleBtn.addTarget(self, action: #selector(titleBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    @objc private func leftBtnClick(){
        print("leftBtnClick")
    }
    
    @objc private func rightBtnClick(){
        print("rightBtnClick")
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        present(vc!, animated: true, completion: nil)
    }
    
    @objc private func titleBtnClick(btn: TitleButton) {
        
        let sb = UIStoryboard.init(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        // 2.1  设置过渡动画的代理
        // 默认情况下modal会移除之前的控制器
        // 如果自定义 就不会移除
        vc?.transitioningDelegate = popoverAnimator
        // 2.2  设置过渡动画的样式
        vc?.modalPresentationStyle = UIModalPresentationStyle.custom
        
        present(vc!, animated: true, completion: nil)
        
        
    }
    
    private lazy var popoverAnimator: PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.delegate = self
        pa.presentFrame = CGRect(x: 100, y: 56, width: 200, height: 350)
        return pa
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
extension HomeTableViewController: PopoverAnimatorDelegate {
    
    func change() {
        // 修改标题按钮状态
        let titleBtn = navigationItem.titleView as! TitleButton
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
}


